//
//  YuanXinSystemPermissions.m
//  YuanXinSystemPermissions
//
//  Created by 晏德智 on 2017/2/7.
//  Copyright © 2017年 晏德智. All rights reserved.
//

#import "YuanXinSystemPermissionsManager.h"
#import "PermissionType.h"
//#import "RCTBridge.h"
//#import "RCTUtils.h"

#import "RCTConvert+PermissionType.h"
#import "SystemPermissionsManager.h"

@implementation YuanXinSystemPermissionsManager

RCT_EXPORT_MODULE();

//@synthesize bridge = _bridge;

#pragma mark Initialization

- (instancetype)init
{
    if (self = [super init]) {
    }
    return self;
}

- (dispatch_queue_t)methodQueue {
    return dispatch_get_main_queue();
}

RCT_EXPORT_METHOD(canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    resolve(@(UIApplicationOpenSettingsURLString != nil));
}

RCT_EXPORT_METHOD(openSettings)
{
    if (@(UIApplicationOpenSettingsURLString != nil)) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:url];
    }
}

RCT_EXPORT_METHOD(getPermissionStatus:(PermissionType)type resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
    
    @try {
        NSString *status = [[SystemPermissionsManager shareInstance] getPermissionStatus:type];
        resolve(status);
    } @catch (NSException *exception) {
        NSError *error = [self NewNSErrorFromException:exception handlerDomain:@"getPermissionStatus" errorCode:@(-1)];
        reject(@(error.code).stringValue,error.localizedDescription,error);
    }
}

RCT_EXPORT_METHOD(requestPermission:(PermissionType)type option:(id)option resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
      @try {
          RCTPromiseResolveBlock successCallback  =nil;
          if(resolve){
              successCallback = [resolve copy];
          }
          
          [[SystemPermissionsManager shareInstance] requestPermission:type
                                                               option:option
                                                    completionHandler:^(NSString *state) {
                                                        if(successCallback){
                                                            successCallback(state);
                                                        }
                                                    }];
      } @catch (NSException *exception) {
      }
}


- (NSError *)NewNSErrorFromException:(NSException *)exc
                       handlerDomain:(NSString *)domain
                       errorCode:(NSNumber *)code {
    NSMutableDictionary * info = [NSMutableDictionary dictionary];
    [info setValue:exc.name forKey:@"MONExceptionName"];
    [info setValue:exc.reason forKey:@"MONExceptionReason"];
    [info setValue:exc.callStackReturnAddresses forKey:@"MONExceptionCallStackReturnAddresses"];
    [info setValue:exc.callStackSymbols forKey:@"MONExceptionCallStackSymbols"];
    [info setValue:exc.userInfo forKey:@"MONExceptionUserInfo"];
    
    return [[NSError alloc] initWithDomain:domain code:code.integerValue userInfo:info];
}

@end
